<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Junit4,单元测试," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="毋庸置疑，程序员要对自己编写的代码负责，您不仅要保证它能编译，正常运行，而且要满足需求和设计的预期的效果。单元测试正是验证代行为是否满足预期的有效手段之一，单元测试工具JUnit使得这一切变得简单艺术起来。
JUnit的最佳实践：单元测试代码和被测试代码使用一样的包，不同的目录（与Maven推荐的目录结构一致）
开发过程中进行单元测试的目的：

减少排错时间
提升代码质量

JUnit4快速入门J">
<meta property="og:type" content="article">
<meta property="og:title" content="Java单元测试框架-JUnit4">
<meta property="og:url" content="https://zitongchen.github.io/2017/08/05/Java单元测试框架-JUnit4/index.html">
<meta property="og:site_name" content="Tony's Notes">
<meta property="og:description" content="毋庸置疑，程序员要对自己编写的代码负责，您不仅要保证它能编译，正常运行，而且要满足需求和设计的预期的效果。单元测试正是验证代行为是否满足预期的有效手段之一，单元测试工具JUnit使得这一切变得简单艺术起来。
JUnit的最佳实践：单元测试代码和被测试代码使用一样的包，不同的目录（与Maven推荐的目录结构一致）
开发过程中进行单元测试的目的：

减少排错时间
提升代码质量

JUnit4快速入门J">
<meta property="og:image" content="https://static.oschina.net/uploads/img/201708/13213928_2jbd.png">
<meta property="og:image" content="https://static.oschina.net/uploads/img/201708/13213940_Uw6O.png">
<meta property="og:image" content="https://static.oschina.net/uploads/img/201708/13213951_y2KY.png">
<meta property="og:image" content="https://static.oschina.net/uploads/img/201708/13214004_YfG9.png">
<meta property="og:image" content="https://static.oschina.net/uploads/img/201708/13214029_9OaQ.png">
<meta property="og:image" content="https://static.oschina.net/uploads/img/201708/13214101_3W2U.gif">
<meta property="og:image" content="https://static.oschina.net/uploads/img/201708/13214112_JQKI.gif">
<meta property="og:image" content="https://zitongchen.github.io/images/JUnit_6.png">
<meta property="og:updated_time" content="2017-09-16T10:22:20.718Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java单元测试框架-JUnit4">
<meta name="twitter:description" content="毋庸置疑，程序员要对自己编写的代码负责，您不仅要保证它能编译，正常运行，而且要满足需求和设计的预期的效果。单元测试正是验证代行为是否满足预期的有效手段之一，单元测试工具JUnit使得这一切变得简单艺术起来。
JUnit的最佳实践：单元测试代码和被测试代码使用一样的包，不同的目录（与Maven推荐的目录结构一致）
开发过程中进行单元测试的目的：

减少排错时间
提升代码质量

JUnit4快速入门J">
<meta name="twitter:image" content="https://static.oschina.net/uploads/img/201708/13213928_2jbd.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zitongchen.github.io/2017/08/05/Java单元测试框架-JUnit4/"/>





  <title> Java单元测试框架-JUnit4 | Tony's Notes </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tony's Notes</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://zitongchen.github.io/2017/08/05/Java单元测试框架-JUnit4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tony">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avater.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tony's Notes">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Java单元测试框架-JUnit4
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-05T00:00:00+08:00">
                2017-08-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Junit4/" itemprop="url" rel="index">
                    <span itemprop="name">Junit4</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/08/05/Java单元测试框架-JUnit4/" class="leancloud_visitors" data-flag-title="Java单元测试框架-JUnit4">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>毋庸置疑，程序员要对自己编写的代码负责，您不仅要保证它能编译，正常运行，而且要满足需求和设计的预期的效果。单元测试正是验证代行为是否满足预期的有效手段之一，单元测试工具JUnit使得这一切变得简单艺术起来。</p>
<p>JUnit的最佳实践：单元测试代码和被测试代码使用一样的包，不同的目录（与Maven推荐的目录结构一致）</p>
<p>开发过程中进行单元测试的目的：</p>
<ul>
<li>减少排错时间</li>
<li>提升代码质量</li>
</ul>
<h2 id="JUnit4快速入门"><a href="#JUnit4快速入门" class="headerlink" title="JUnit4快速入门"></a>JUnit4快速入门</h2><p>JUnit4是xUnit的子集，xUnit是一套基于测试驱动开发的测试框架</p>
<p>JUnit4提过一系列判断测试结果是否正确的静态断言方法-Assert</p>
<p>测试类的规范：</p>
<ul>
<li>测试方法必须使用注解@Test修饰</li>
</ul>
<ul>
<li>测试方法必须使用public void 修饰，而且不能带有任何参数</li>
</ul>
<p>需要测试的类：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Calculate</span> </span>&#123;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">		<span class="keyword">return</span> a + b;</div><div class="line">	&#125;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">subtract</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">		<span class="keyword">return</span> a - b;</div><div class="line">	&#125;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">multiply</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">		<span class="keyword">return</span> a * b;</div><div class="line">	&#125;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">divide</span><span class="params">(<span class="keyword">int</span> a ,<span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">		<span class="keyword">return</span> a / b;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在Eclipse中建立相应类的测试单元很简单：</p>
<ul>
<li>第一步</li>
</ul>
<p><img src="https://static.oschina.net/uploads/img/201708/13213928_2jbd.png" alt="输入图片说明" title="在这里输入图片标题"></p>
<ul>
<li>第二步</li>
</ul>
<p><img src="https://static.oschina.net/uploads/img/201708/13213940_Uw6O.png" alt="输入图片说明" title="在这里输入图片标题"></p>
<ul>
<li>第三步</li>
</ul>
<p><img src="https://static.oschina.net/uploads/img/201708/13213951_y2KY.png" alt="输入图片说明" title="在这里输入图片标题"></p>
<p>单元测试类：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CalculateTest</span> </span>&#123;</div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testAdd</span><span class="params">()</span> </span>&#123;</div><div class="line">		assertEquals(<span class="number">6</span>, <span class="keyword">new</span> Calculate().add(<span class="number">3</span>, <span class="number">3</span>));<span class="comment">//使用断言测试期望输出与实际输出是否一致</span></div><div class="line">	&#125;</div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSubtract</span><span class="params">()</span> </span>&#123;</div><div class="line">		assertEquals(<span class="number">3</span>, <span class="keyword">new</span> Calculate().subtract(<span class="number">6</span>, <span class="number">3</span>));</div><div class="line">	&#125;</div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testMultiply</span><span class="params">()</span> </span>&#123;</div><div class="line">		assertEquals(<span class="number">9</span>, <span class="keyword">new</span> Calculate().multiply(<span class="number">3</span>, <span class="number">3</span>));</div><div class="line">	&#125;</div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testDivide</span><span class="params">()</span> </span>&#123;</div><div class="line">		assertEquals(<span class="number">3</span>, <span class="keyword">new</span> Calculate().divide(<span class="number">9</span>, <span class="number">3</span>));</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="JUnit4使用详解"><a href="#JUnit4使用详解" class="headerlink" title="JUnit4使用详解"></a>JUnit4使用详解</h2><p>JUnit最佳实践：测试任何可能的错误，单元测试不是证明你是对的，而是用来证明你没有错。</p>
<p>单元测试的范围要全面，比如对边界值，正常值，错误值的测试；对代码可能出现的问题全面预测，而这也是需求分析，详细设计环节要考虑的。</p>
<h3 id="JUnit测试失败的情况分两种：failure-和-error"><a href="#JUnit测试失败的情况分两种：failure-和-error" class="headerlink" title="JUnit测试失败的情况分两种：failure 和 error"></a>JUnit测试失败的情况分两种：failure 和 error</h3><ul>
<li>failure 一般由单元测试使用的断言方法判断失败引起的，它表示在测试点发现了问题</li>
</ul>
<p><img src="https://static.oschina.net/uploads/img/201708/13214004_YfG9.png" alt="输入图片说明" title="在这里输入图片标题"></p>
<ul>
<li>error则是由代码异常引起的，这是测试目的之外的发现，它可能产生与测试代码本身的错误(测试代码也是代码，也有可能出错)，也可能是被测试代码的一个隐藏bug。</li>
</ul>
<p><img src="https://static.oschina.net/uploads/img/201708/13214029_9OaQ.png" alt="  ![](images/JUnit_5.png)" title="在这里输入图片标题"></p>
<p>单元用例用来表达想要的预期结果，但对于逻辑错误无能为力</p>
<h3 id="JUnit4的运行流程"><a href="#JUnit4的运行流程" class="headerlink" title="JUnit4的运行流程"></a>JUnit4的运行流程</h3><ol>
<li><p>@BeforeClass修饰的方法会在所有方法被调用前被执行，而且该方法是静态的，所以当测试类被加载后接着就会运行它，而且在内存中它只会存在一份实例，它比较适合加载配置文件。</p>
</li>
<li><p>@AfterClass所修饰的方法通常用来对资源的清理，如关闭数据库的连接</p>
</li>
<li><p>@Before和@After会在每个测试方法的前后各执行一次。</p>
<p>类级别的运行流程</p>
</li>
</ol>
<p><img src="https://static.oschina.net/uploads/img/201708/13214101_3W2U.gif" alt="   ![](images/JUnit_figure006.gif)" title="在这里输入图片标题"></p>
<p>   方法级别的运行流程</p>
<p><img src="https://static.oschina.net/uploads/img/201708/13214112_JQKI.gif" alt="输入图片说明" title="在这里输入图片标题"></p>
<h3 id="JUnit4的常用注解"><a href="#JUnit4的常用注解" class="headerlink" title="JUnit4的常用注解"></a>JUnit4的常用注解</h3><ul>
<li><p>@Test:将一个普通的方法修饰为一个测试方法</p>
<p>​    @Test(expected=异常类.class)</p>
<p>​        显示了测试方法会出现的异常后，当测试方法抛出此异常后会，该测试方法依然能通过测试</p>
<p>​    @Test(timeout = 毫秒)</p>
<p>​        作用于测试方法的执行是否超时，若超时则测试不通过</p>
</li>
<li><p>@BeforeClass:它会在所以方法运行前被执行，static修饰</p>
<p>​    用于引入一些资源文件等</p>
</li>
<li><p>@AfterClass:它会在所以方法运行结束后被执行，static修饰、</p>
<p>​    用于方法执行完毕以后关闭资源，例如数据库连接等</p>
</li>
<li><p>@Before: 会在每个测试方法被运行前执行一次</p>
</li>
<li><p>@After: 会在每个测试方法运行后执行一次</p>
</li>
<li><p>@Ignore: 所修饰的测试方法会被测试运行器忽略</p>
</li>
<li><p>@RunWith：可以更改测试运行器，自定义的运行器需继承org.junit.runner.Runner</p>
<p><img src="images/JUnit_6.png" alt=""></p>
</li>
</ul>
<h2 id="JUnit4的深入使用"><a href="#JUnit4的深入使用" class="headerlink" title="JUnit4的深入使用"></a>JUnit4的深入使用</h2><p>JUnit4中的测试套件，运行多个测试类，测试套件是一个空类，使用@RunWith(测试套件类.class)</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="meta">@RunWith</span>(Suite.class)</div><div class="line"><span class="meta">@Suite</span>.SuiteClasses(&#123;TaskTest1.class,TaskTest2.class,TaskTest3.class&#125;)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SuiteTest</span> </span>&#123;</div><div class="line">	<span class="comment">/*</span></div><div class="line">	 * 1.测试套件就是组织测试类一起运行的</div><div class="line">	 * </div><div class="line">	 * 写一个作为测试套件的入口类，这个类里不包含其他的方法</div><div class="line">	 * 更改测试运行器Suite.class</div><div class="line">	 * 将要测试的类作为数组传入到Suite.SuiteClasses（&#123;&#125;）</div><div class="line">	 */</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>JUnit4的参数化测试</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="meta">@RunWith</span>(Parameterized.class)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ParameterTest</span> </span>&#123;</div><div class="line">	<span class="comment">/*</span></div><div class="line">	 * 1.更改默认的测试运行器为RunWith(Parameterized.class)</div><div class="line">	 * 2.声明变量来存放预期值 和结果值</div><div class="line">	 * 3.声明一个返回值 为Collection的公共静态方法，并使用@Parameters进行修饰</div><div class="line">	 * 4.为测试类声明一个带有参数的公共构造函数，并在其中为之声明变量赋值</div><div class="line">	 */</div><div class="line">	<span class="keyword">int</span> expected =<span class="number">0</span>;</div><div class="line">	<span class="keyword">int</span> input1 = <span class="number">0</span>;</div><div class="line">	<span class="keyword">int</span> input2 = <span class="number">0</span>;</div><div class="line">	<span class="meta">@Parameters</span></div><div class="line">	<span class="keyword">public</span> <span class="keyword">static</span> Collection&lt;Object[]&gt; t() &#123;</div><div class="line">		<span class="keyword">return</span> Arrays.asList(<span class="keyword">new</span> Object[][]&#123;</div><div class="line">				&#123;<span class="number">3</span>,<span class="number">1</span>,<span class="number">2</span>&#125;,</div><div class="line">				&#123;<span class="number">4</span>,<span class="number">2</span>,<span class="number">2</span>&#125;</div><div class="line">		&#125;) ;</div><div class="line">	&#125;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ParameterTest</span><span class="params">(<span class="keyword">int</span> expected,<span class="keyword">int</span> input1,<span class="keyword">int</span> input2)</span> </span>&#123;</div><div class="line">		<span class="keyword">this</span>.expected = expected;</div><div class="line">		<span class="keyword">this</span>.input1 = input1;</div><div class="line">		<span class="keyword">this</span>.input2 = input2;</div><div class="line">	&#125;</div><div class="line">	<span class="meta">@Test</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testAdd</span><span class="params">()</span> </span>&#123;</div><div class="line">		assertEquals(expected, <span class="keyword">new</span> Calculate().add(input1, input2));</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="JUnit4在web项目中的使用"><a href="#JUnit4在web项目中的使用" class="headerlink" title="JUnit4在web项目中的使用"></a>JUnit4在web项目中的使用</h2><p>Web项目中测试类的整合，测试运行器也用spring框架的运行器</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</div><div class="line"><span class="meta">@ContextConfiguration</span>(&#123;<span class="string">"classpath:spring/spring-dao.xml"</span>,</div><div class="line">                        <span class="string">"classpath:spring/spring-service.xml"</span>&#125;)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SeckillServiceTest</span> </span>&#123;</div><div class="line">   	<span class="comment">//.......</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>测试用例建立在业务逻辑的正确性上，需要对业务有深刻的理解，才能写出有效的测试用例。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Junit4/" rel="tag"># Junit4</a>
          
            <a href="/tags/单元测试/" rel="tag"># 单元测试</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/01/Java日志学习笔记/" rel="next" title="Java日志学习笔记">
                <i class="fa fa-chevron-left"></i> Java日志学习笔记
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/10/细说Java多线程之内存可见性/" rel="prev" title="细说Java多线程之内存可见性">
                细说Java多线程之内存可见性 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="uyan_frame"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avater.png"
               alt="Tony" />
          <p class="site-author-name" itemprop="name">Tony</p>
           
              <p class="site-description motion-element" itemprop="description">学习技术，分享技术...</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">25</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/zitongchen" rel="external nofollow" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JUnit4快速入门"><span class="nav-number">1.</span> <span class="nav-text">JUnit4快速入门</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JUnit4使用详解"><span class="nav-number">2.</span> <span class="nav-text">JUnit4使用详解</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#JUnit测试失败的情况分两种：failure-和-error"><span class="nav-number">2.1.</span> <span class="nav-text">JUnit测试失败的情况分两种：failure 和 error</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JUnit4的运行流程"><span class="nav-number">2.2.</span> <span class="nav-text">JUnit4的运行流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JUnit4的常用注解"><span class="nav-number">2.3.</span> <span class="nav-text">JUnit4的常用注解</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JUnit4的深入使用"><span class="nav-number">3.</span> <span class="nav-text">JUnit4的深入使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JUnit4在web项目中的使用"><span class="nav-number">4.</span> <span class="nav-text">JUnit4在web项目中的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tony</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io" rel="external nofollow">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  
    

    
      <!-- UY BEGIN -->
      <script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=2130825"></script>
      <!-- UY END -->
    
  





  



  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("4wyUmphSKE5wrQGHEh5Js0bk-gzGzoHsz", "oTOHrhMuED5U0oqV2KrRMGF0");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  


  
</body>
</html>
